package com.azt.front.action.enquiry;

import com.azt.api.dto.*;
import com.azt.api.enums.EnqEnquiryEnum;
import com.azt.api.enums.EnqPaymentEnum;
import com.azt.api.pojo.*;
import com.azt.api.service.CompanyService;
import com.azt.api.service.EnqEnquiryService;
import com.azt.api.service.EnqQuoteService;
import com.azt.front.action.enquiry.model.EnqBgetPchaseSaveWrap;
import com.azt.front.action.enquiry.model.EnqRecommendChoose;
import com.azt.front.permission.RequireUser;
import com.azt.front.utils.ContextUtil;
import com.azt.model.AjaxJson;
import com.azt.model.page.TPagination;
import com.azt.utils.BeanUtils;
import com.azt.utils.ExcelUtil;
import com.azt.utils.FreemarkerUtils;
import com.azt.utils.RequestUtils;
import freemarker.template.TemplateModel;
import org.apache.commons.collections.map.HashedMap;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import static com.azt.front.utils.Globals.PAGESIZE;

/**
 * 寻价报价
 * Created by 张栋 on 2016/9/1811:06
 */
@Controller
public class EnquiryActionExt {
    @Autowired
    private EnqEnquiryService enq;


    /**
     * 生成报价清单 Workbook 或者  zip
     * Created by 张栋 on 2016/10/20 10:56
     */
    private static DecimalFormat decimalFormat = new DecimalFormat("0.####");
    private static DecimalFormat decimalFormat2 = new DecimalFormat("0.##");

    public Workbook getWorkbook(Integer quoteid, ZipOutputStream zipout) throws IOException {
        EnqQuote quote = enq.getEnqQuoteById(quoteid);
        String sheetName = quote.getCompanyName();
        if(zipout!=null){
            zipout.putNextEntry(new ZipEntry(sheetName+".xls"));
        }else{
             sheetName = sheetName+".xls";
        }

        TemplateExportParams params = new TemplateExportParams("excel/报价清单.xls");
        params.setColForEach(true);
        params.setSheetName(sheetName);
        Map<String, Object> map = new HashMap<String, Object>();
        List<EnqQuoteItemExt> quoteitem = enq.getEnqQuoteItemExt(quoteid);
        List<Map<String,Object>> maplist = new ArrayList<>();
        int i=0;
        for (EnqQuoteItemExt ext:quoteitem){
            Map<String,Object> m = new HashedMap();
            m.put("xuhao",(i+1)+"");
            m.put("productName",ext.getProductName());
            m.put("brand",ext.getBrand());
            m.put("model",ext.getModel());
            m.put("unit",ext.getUnit());
            m.put("unitprice",decimalFormat2.format(ext.getUnitprice()));
            m.put("quantity",decimalFormat.format(ext.getQuantity()));
            m.put("totalPrice",decimalFormat2.format(ext.getTotalPrice()));
            m.put("descr",ext.getDescr());
            maplist.add(m);
            i++;
        }
        map.put("list",maplist);
        Workbook wb = ExcelExportUtil.exportExcel(params, map);
        if(zipout!=null){
            wb.write(zipout);
            zipout.closeEntry();
        }
        return wb;
    }

    /**
     * 生成预算清单workbook
     * Created by 张栋 on 2016/10/20 10:57
     */
    public Workbook  getBgetWorkBbook(List<EnqQuoteItemExt> detailList){

        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String,Object>> maplist = new ArrayList<>();
        int i=0;
        for (EnqQuoteItemExt ext:detailList){
            Map<String,Object> m = new HashedMap();
            m.put("xuhao",(i+1)+"");
            m.put("companyName",ext.getCompanyName());
            m.put("productName",ext.getProductName());
            m.put("brand",ext.getBrand());
            m.put("categoryName",ext.getCategoryName());
            m.put("quantity",ext.getQuantity()+"");
            m.put("unit",ext.getUnit());
            m.put("unitprice",ext.getUnitprice());
            m.put("totalPrice",ext.getTotalPrice());
            maplist.add(m);
            i++;
        }

        TemplateExportParams params = new TemplateExportParams("excel/预算清单.xls");
        params.setColForEach(true);
        map.put("list",maplist);
        Workbook wb = ExcelExportUtil.exportExcel(params, map);
        return wb;
    }
}
